{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python361264bittfconda6792a3e09dd440c78bca3d69354576cc",
   "display_name": "Python 3.6.12 64-bit ('tf': conda)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第2讲：RNN网络样本的生成方法\n",
    "## Step1: 生成RNN网络样本数据集"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'2.0.0-beta0'"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "# 导入\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import tensorflow as tf\n",
    "\n",
    "plt.style.use(['science', 'grid', 'muted'])\n",
    "\n",
    "tf.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 "
     ]
    }
   ],
   "source": [
    "# 生成序列数据\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "for val in dataset:\n",
    "    print(val.numpy(), end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0 1 2 3 4 \n",
      "1 2 3 4 5 \n",
      "2 3 4 5 6 \n",
      "3 4 5 6 7 \n",
      "4 5 6 7 8 \n",
      "5 6 7 8 9 \n",
      "6 7 8 9 \n",
      "7 8 9 \n",
      "8 9 \n",
      "9 \n"
     ]
    }
   ],
   "source": [
    "# 获取窗口数据，窗口大小为5\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "dataset = dataset.window(5, shift=1)\n",
    "for window_dataset in dataset:\n",
    "    for val in window_dataset:\n",
    "        print(val.numpy(), end=' ')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0 1 2 3 4 \n1 2 3 4 5 \n2 3 4 5 6 \n3 4 5 6 7 \n4 5 6 7 8 \n5 6 7 8 9 \n"
     ]
    }
   ],
   "source": [
    "# 去掉不完整的数据\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "dataset = dataset.window(5, shift=1, drop_remainder=True)\n",
    "for window_dataset in dataset:\n",
    "    for val in window_dataset:\n",
    "        print(val.numpy(), end=' ')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[0 1 2 3 4]\n[1 2 3 4 5]\n[2 3 4 5 6]\n[3 4 5 6 7]\n[4 5 6 7 8]\n[5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 转为numpy列表\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "dataset = dataset.window(5, shift=1, drop_remainder=True)\n",
    "dataset = dataset.flat_map(lambda window: window.batch(5))\n",
    "for window in dataset:\n",
    "    print(window.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[0 1 2 3] [4]\n[1 2 3 4] [5]\n[2 3 4 5] [6]\n[3 4 5 6] [7]\n[4 5 6 7] [8]\n[5 6 7 8] [9]\n"
     ]
    }
   ],
   "source": [
    "# 打乱数据\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "dataset = dataset.window(5, shift=1, drop_remainder=True)\n",
    "dataset = dataset.flat_map(lambda window: window.batch(5)) # 每5个为1批\n",
    "dataset = dataset.map(lambda window: (window[:-1], window[-1:])) # 模拟生成特征和标签\n",
    "dataset = dataset.shuffle(buffer_size=10) # 打乱\n",
    "for X, y in dataset:\n",
    "    print(X.numpy(), y.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Batch 0: X = [[ 8  9 10 11]\n [ 1  2  3  4]\n [ 0  1  2  3]\n [ 7  8  9 10]\n [10 11 12 13]\n [ 2  3  4  5]\n [13 14 15 16]\n [16 17 18 19]\n [15 16 17 18]\n [11 12 13 14]\n [17 18 19 20]\n [12 13 14 15]\n [ 4  5  6  7]\n [ 3  4  5  6]\n [18 19 20 21]\n [20 21 22 23]\n [ 9 10 11 12]\n [ 5  6  7  8]\n [21 22 23 24]\n [22 23 24 25]] \n y = [[12]\n [ 5]\n [ 4]\n [11]\n [14]\n [ 6]\n [17]\n [20]\n [19]\n [15]\n [21]\n [16]\n [ 8]\n [ 7]\n [22]\n [24]\n [13]\n [ 9]\n [25]\n [26]]\nBatch 1: X = [[28 29 30 31]\n [ 6  7  8  9]\n [30 31 32 33]\n [25 26 27 28]\n [26 27 28 29]\n [24 25 26 27]\n [29 30 31 32]\n [36 37 38 39]\n [34 35 36 37]\n [27 28 29 30]\n [31 32 33 34]\n [37 38 39 40]\n [39 40 41 42]\n [33 34 35 36]\n [41 42 43 44]\n [35 36 37 38]\n [44 45 46 47]\n [14 15 16 17]\n [45 46 47 48]\n [40 41 42 43]] \n y = [[32]\n [10]\n [34]\n [29]\n [30]\n [28]\n [33]\n [40]\n [38]\n [31]\n [35]\n [41]\n [43]\n [37]\n [45]\n [39]\n [48]\n [18]\n [49]\n [44]]\nBatch 2: X = [[48 49 50 51]\n [46 47 48 49]\n [23 24 25 26]\n [42 43 44 45]\n [38 39 40 41]\n [49 50 51 52]\n [51 52 53 54]\n [54 55 56 57]\n [50 51 52 53]\n [32 33 34 35]\n [53 54 55 56]\n [60 61 62 63]\n [58 59 60 61]\n [62 63 64 65]\n [55 56 57 58]\n [63 64 65 66]\n [52 53 54 55]\n [43 44 45 46]\n [64 65 66 67]\n [65 66 67 68]] \n y = [[52]\n [50]\n [27]\n [46]\n [42]\n [53]\n [55]\n [58]\n [54]\n [36]\n [57]\n [64]\n [62]\n [66]\n [59]\n [67]\n [56]\n [47]\n [68]\n [69]]\nBatch 3: X = [[66 67 68 69]\n [59 60 61 62]\n [57 58 59 60]\n [72 73 74 75]\n [70 71 72 73]\n [69 70 71 72]\n [56 57 58 59]\n [61 62 63 64]\n [76 77 78 79]\n [47 48 49 50]\n [77 78 79 80]\n [80 81 82 83]\n [19 20 21 22]\n [68 69 70 71]\n [71 72 73 74]\n [83 84 85 86]\n [81 82 83 84]\n [79 80 81 82]\n [78 79 80 81]\n [86 87 88 89]] \n y = [[70]\n [63]\n [61]\n [76]\n [74]\n [73]\n [60]\n [65]\n [80]\n [51]\n [81]\n [84]\n [23]\n [72]\n [75]\n [87]\n [85]\n [83]\n [82]\n [90]]\nBatch 4: X = [[75 76 77 78]\n [84 85 86 87]\n [74 75 76 77]\n [67 68 69 70]\n [90 91 92 93]\n [85 86 87 88]\n [89 90 91 92]\n [91 92 93 94]\n [82 83 84 85]\n [93 94 95 96]\n [73 74 75 76]\n [95 96 97 98]\n [94 95 96 97]\n [88 89 90 91]\n [87 88 89 90]\n [92 93 94 95]] \n y = [[79]\n [88]\n [78]\n [71]\n [94]\n [89]\n [93]\n [95]\n [86]\n [97]\n [77]\n [99]\n [98]\n [92]\n [91]\n [96]]\n"
     ]
    }
   ],
   "source": [
    "# 设置数据批量\n",
    "dataset = tf.data.Dataset.range(10)\n",
    "dataset = dataset.window(5, shift=1, drop_remainder=True)\n",
    "dataset = dataset.flat_map(lambda window: window.batch(5)) # 每5个为1项数据\n",
    "dataset = dataset.map(lambda window: (window[:-1], window[-1:])) # 模拟生成特征和标签\n",
    "dataset = dataset.shuffle(buffer_size=10) # 打乱\n",
    "dataset = dataset.batch(2).prefetch(1) # 设置数据批量\n",
    "for batch_num, (X, y) in enumerate(dataset):\n",
    "    print(\"Batch {}: X = {} \\n y = {}\".format(batch_num, X.numpy(), y.numpy()))"
   ]
  }
 ]
}